package com.ia.aistream.manager.common.impl;

import com.ia.aistream.common.config.Global;
import com.ia.aistream.common.config.ServerConfig;
import com.ia.aistream.common.core.text.Convert;
import com.ia.aistream.common.utils.DateUtils;
import com.ia.aistream.common.utils.file.FileUploadUtils;
import com.ia.aistream.common.utils.file.dto.FileDataDto;
import com.ia.aistream.manager.common.ICommonFileManager;
import com.ia.aistream.mapper.common.CommonFileMapper;
import com.ia.aistream.model.common.entity.CommonFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 系统文件路径Service业务层处理
 * 
 * @author aistream
 * @date 2020-12-07
 */
@Component
public class CommonFileManagerImpl implements ICommonFileManager
{
    @Autowired
    private CommonFileMapper commonFileMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询系统文件路径
     * 
     * @param id 系统文件路径ID
     * @return 系统文件路径
     */
    @Override
    public CommonFile selectCommonFileById(Long id)
    {
        return commonFileMapper.selectCommonFileById(id);
    }

    /**
     * 查询系统文件路径列表
     * 
     * @param commonFile 系统文件路径
     * @return 系统文件路径
     */
    @Override
    public List<CommonFile> selectCommonFileList(CommonFile commonFile)
    {
        return commonFileMapper.selectCommonFileList(commonFile);
    }

    /**
     * 新增系统文件路径
     * 
     * @param commonFile 系统文件路径
     * @return 结果
     */
    @Override
    public int insertCommonFile(CommonFile commonFile)
    {
        commonFile.setCreateTime(DateUtils.getNowDate());
        return commonFileMapper.insertCommonFile(commonFile);
    }

    /**
     * 修改系统文件路径
     * 
     * @param commonFile 系统文件路径
     * @return 结果
     */
    @Override
    public int updateCommonFile(CommonFile commonFile)
    {
        return commonFileMapper.updateCommonFile(commonFile);
    }

    /**
     * 删除系统文件路径对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCommonFileByIds(String ids)
    {
        return commonFileMapper.deleteCommonFileByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统文件路径信息
     * 
     * @param id 系统文件路径ID
     * @return 结果
     */
    @Override
    public int deleteCommonFileById(Long id)
    {
        return commonFileMapper.deleteCommonFileById(id);
    }

    @Override
    public CommonFile upload(MultipartFile file) throws Exception{
        CommonFile commonFile = new CommonFile();
        // 上传文件路径
        String filePath = Global.getProfile();

        // 上传并返回新文件名称
        FileDataDto fileDataDto = FileUploadUtils.upload(filePath, file);

        BeanUtils.copyProperties(fileDataDto,commonFile);
        commonFileMapper.insert(commonFile);
        commonFile.setDownloadUrl(serverConfig.getUrl() + "/common/file/download" + commonFile.getDownloadUrl());
        return commonFile;
    }

    @Override
    public CommonFile upload(MultipartFile file, String[] allowedExtension) throws Exception {
        CommonFile commonFile = new CommonFile();
        // 上传文件路径
        String filePath = Global.getProfile();

        // 上传并返回新文件名称
        FileDataDto fileDataDto = FileUploadUtils.uploadFileName(filePath, file, allowedExtension);

        BeanUtils.copyProperties(fileDataDto,commonFile);
        commonFileMapper.insert(commonFile);
        commonFile.setDownloadUrl(serverConfig.getUrl() + "/common/file/download" + commonFile.getDownloadUrl());

        return commonFile;
    }

    @Override
    public CommonFile batchUpload(MultipartFile files) throws Exception {

        CommonFile commonFile = new CommonFile();
        // 上传文件路径
        String filePath = Global.getProfile();

        String[] allowedExtension = { "dwg", "dxf" };

        // 上传并返回新文件名称
        FileDataDto fileDataDto = FileUploadUtils.uploadFileName(filePath, files,allowedExtension);

        BeanUtils.copyProperties(fileDataDto,commonFile);
        commonFileMapper.insert(commonFile);
        commonFile.setDownloadUrl(serverConfig.getUrl() + "/common/file/download" + commonFile.getDownloadUrl());

        return commonFile;
    }

    @Override
    public CommonFile wordUpload(MultipartFile file) throws Exception {
        CommonFile commonFile = new CommonFile();
        // 上传文件路径
        String filePath = Global.getProfile();

        String[] allowedExtension = { "doc", "docx" };
        // 上传并返回新文件名称
        FileDataDto fileDataDto = FileUploadUtils.uploadWordFileName(filePath, file, allowedExtension);

        BeanUtils.copyProperties(fileDataDto,commonFile);
        commonFileMapper.insert(commonFile);
        commonFile.setDownloadUrl(serverConfig.getUrl() + "/common/file/download" + commonFile.getDownloadUrl());
        return commonFile;
    }

}
